package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.Article;
import com.briup.jz.bean.ArticleExample;
import com.briup.jz.bean.extend.ArticleExtend;
import com.briup.jz.dao.ArticleMapper;
import com.briup.jz.dao.extend.ArticleExtendMapper;
import com.briup.jz.service.IArticleService;


//@Service 声明这是一个实现类
@Service
public class ArticleServiceimpl implements IArticleService{
	
	@Autowired
	private ArticleMapper articleMapper;
	@Autowired
	private ArticleExtendMapper articleExtendMapper;

	@Override
	public void saveOrUpdate(Article a1) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				if(a1.getId()==null) {
					//初始化
					a1.setPublishTime(new Date().getTime());
					a1.setReadTimes(0l);
					a1.setThumpUp(0l);
					a1.setStatus("未审核");
					//插入数据库
					articleMapper.insert(a1);
				}else {
					articleMapper.updateByPrimaryKey(a1);
				}
	}

	@Override
	public List<Article> query(String title, String status, Long categoryId) {
		// TODO Auto-generated method stub
		// 模板查询
				ArticleExample example = new ArticleExample();
				//条件
				ArticleExample.Criteria criteria = example.createCriteria();
				if(title !=null) {
					criteria.andTitleLike("%"+title+"%");
				}
				if(status != null ) {
					criteria.andStatusEqualTo(status);
				}
				if(categoryId !=null) {
					criteria.andCategoryIdEqualTo(categoryId);
				}
				return articleMapper.selectByExample(example);
	}

	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		articleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<ArticleExtend> queryCascade(String title, String status, Long categoryId) {
		// TODO Auto-generated method stub
		return articleExtendMapper.select(title, status, categoryId);
	}

	@Override
	public ArticleExtend read(long id) {
		ArticleExtend article = articleExtendMapper.selectById(id);
		// 修改阅读次数
		article.setReadTimes(article.getReadTimes()+1);
		articleMapper.updateByPrimaryKey(article);
		//根据ID查询文章
		
		return article;
	}

	@Override
	public List<ArticleExtend> pageQuery(int page, int pageSize, String title, String status, Long categoryId) {
		int pageIndex = (page-1)*pageSize;
		return articleExtendMapper.pageQuery(pageIndex,pageSize,title,status,categoryId);
	}
	
}


